/*
 * fbank.com Inc.
 * Copyright (c) 2020 All Rights Reserved.
 * create by fm
 * date:2020-04-26
 */
package com.ouwen.smartpay.service.impl;

import com.fm.core.common.service.EntityServiceImpl;
import com.ouwen.smartpay.dao.PartnerDao;
import com.ouwen.smartpay.entity.Partner;
import com.ouwen.smartpay.service.PartnerService;
import com.ouwen.smartpay.service.cache.CacheKeys;
import com.ouwen.smartpay.service.cache.ICacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 商户管理 Service实现
 * <p>
 * Date: 2020-04-26 11:00:17
 *
 * @author fm
 */
@Service("partnerService")
public class PartnerServiceImpl extends EntityServiceImpl<Partner, PartnerDao> implements PartnerService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ICacheService localCacheService;

    @Override
    public Partner findPartner(String partnerId, String subPartnerId) {

        try {

            return localCacheService.loadCache(CacheKeys.buildKey(CacheKeys.OPENAPI_PARTNER_CACHEKEY,partnerId,subPartnerId), () ->
                    getEntityDao().findPartner(partnerId, subPartnerId)
            );

        } catch (Exception e) {

            logger.debug("商户号【{}】子商户号【{}】缓存不存在:", partnerId,subPartnerId);

            return null;

        }

    }

    @Override
    public void updatePartnerService(Partner partner) {

        localCacheService.evitCache(CacheKeys.buildKey(CacheKeys.OPENAPI_PARTNER_CACHEKEY, partner.getPartnerId(), partner.getSubPartnerId()));

        getEntityDao().update(partner);

    }

}
